package com.imti.retrofitweatherdemo.fragment;

import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.imti.retrofitweatherdemo.NewsListAdapter;
import com.imti.retrofitweatherdemo.R;
import com.imti.retrofitweatherdemo.logic.NewsModelListItem;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class NewsListFragment extends LazyLoadFragment
{

    private SmartRefreshLayout mRefresh;

    private RecyclerView mNewsList;

    private List<NewsModelListItem> mNewsDataList = new ArrayList<>();

    private NewsListAdapter mNewsAdapter;

    @Override
    protected int setContentView()
    {
        return R.layout.fragment_news_list;
    }
    @Override
    protected void lazyLoad()
    {
        if (isInit)
        {
            initView();
        }
    }

    public void initView()
    {
        mRefresh = findViewById(R.id.news_list_refresh);
        mNewsList = findViewById(R.id.news_list);
        mNewsList.setLayoutManager(new LinearLayoutManager(getActivity()));

        mNewsAdapter = new NewsListAdapter(getActivity());

        //离线假数据构造
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

        NewsDataModel.getInstance().dataReady().observe(getActivity(),
            new Observer<List<NewsModelListItem>>()
            {
                @Override
                public void onChanged(List<NewsModelListItem> newsModelListItems)
                {
                    mNewsDataList.clear();
                    mNewsDataList.addAll(newsModelListItems);
                    mNewsAdapter.notifyDataSetChanged();
                    mNewsList.setVisibility(ViewGroup.VISIBLE);
                }
            });

        mNewsAdapter.setNewsModelLists(mNewsDataList);
        mNewsList.setAdapter(mNewsAdapter);
    }


}